package top.huxy.boot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.huxy.boot.BlogApiApplication;
import top.huxy.boot.model.vo.VOVisitorCityInfo;
import top.huxy.boot.service.DashboardService;
import top.huxy.boot.util.DateUtils;
import top.huxy.boot.util.IPAddressUtils;
import top.huxy.boot.util.RequestUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname DashboardServiceImpl
 * @Description DashboardServiceImpl
 * @Date 2021/9/30 23:04
 * @Created by huxy
 */
@Service
@Slf4j
public class DashboardServiceImpl implements DashboardService {

    @Autowired
    VisitLogServiceImpl visitLogService;

    @Autowired
    BlogServiceImpl blogService;

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<VOVisitorCityInfo> getVisitorCityInfo() {
        return visitLogService.getVisitorCityInfo();
    }

    @Override
    public Map<String, List<Object>> getVisitor7() {
        long end = System.currentTimeMillis();
        long start = DateUtils.getDayTime(end - 1000 * 60 * 60 * 24 * 7);
        return visitLogService.getVisitorCountGroupByDays(start, end);
    }

    //    @Override
//    public Map<String, Object> getVisitor30() {
//        Calendar now = Calendar.getInstance();
//        LinkedList<String> dates = new LinkedList<>();
//        LinkedList<Integer> values = new LinkedList<>();
//        for (int i = 0; i < 10; i++) {
//
//            String date1 = DateUtils.format("M/d", now.getTime());
//            int sum = 0;
//            for (int j = 0; j < 3; j++) {
//                //获取这天0点
//                long end = DateUtils.getDayTime(now.getTimeInMillis());
//
//                //倒退一天
//                now.add(Calendar.DAY_OF_MONTH, -1);
//
//                //获取这天0点
//                long startTime = DateUtils.getDayTime(now.getTimeInMillis());
//
//                sum += visitLogService.getVisitorCount(startTime, end);
//            }
//            String date2 = DateUtils.format("M/d", now.getTime());
//
//            dates.addFirst(date2 + "-" + date1);
//            values.addFirst(sum);
//        }
//        HashMap<String, Object> res = new HashMap<>(2);
//        res.put("dates", dates);
//        res.put("values", values);
//        return res;
//    }
    @Override
    public Map<String, List<Object>> getVisitor30() {
        long end = System.currentTimeMillis();
        long start = DateUtils.getDayTime(end - (long) 1000 * 60 * 60 * 24 * 30);
        return visitLogService.getVisitorCountGroupByDays(start, end);
    }

    @Override
    public HashMap<String, Object> getTopData() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("serverStartDate", BlogApiApplication.getServerStartDate());
        result.put("jarName", getJarName());

        String currentIp = IPAddressUtils.getRequestIp();
        result.put("clientIp",currentIp);
        result.put("ipInfo",IPAddressUtils.getCityInfo(currentIp));
        Map<String, String> osAndBrowser = RequestUtils.parseOsAndBrowser(RequestUtils.getUserAgent());
        result.put("clientOs",osAndBrowser.get("os"));
        result.put("clientBrowser",osAndBrowser.get("browser"));

        return result;
    }

    private String getJarName() {
        try {
            String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
            Process process = Runtime.getRuntime().exec("jps");
            try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    if (line.startsWith(pid)) {
                        return line.split(" ")[1];
                    }
                }
            }
            return "";
        } catch (Exception e) {
            log.error("getJarNameError", e);
            return null;
        }
    }

}
